///////////////////////////////////////////////////////////
//  CTemplateView.cpp
//  Implementation of the Class CTemplateView
//  Created on:      21-Sep-2013 9:37:06 AM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "CTemplateView.h"
#include "ControlCommand.h"
#include "stdafx.h"

using namespace UI;
using namespace UI::CMD;

CTemplateView::CTemplateView(IPages* templatePages, IWindowDirector* director)
{
  this->m_type = IViewCtrl::TEMPLATEVIEW_T;
  this->m_parrentWnd = director;
  this->m_templatePages = templatePages;
  this->m_currentRow = -1;

  // call initialize for create all UI.
  Initialize();
}

CTemplateView::~CTemplateView()
{

}

UI::IViewCtrl::ViewType CTemplateView::GetType() const 
{
	return this->m_type;
}

void CTemplateView::Initialize()
{
  // Setting attribute of view
  this->SetAttribute("x1", 0);
  this->SetAttribute("y1", 0);
  this->SetAttribute("width", "100%");
  this->SetAttribute("height", "90%");
  this->SetAttribute("backgroundColour", CColour(COLOUR_PAGE));

  // Create navigationbar and set right button name
  this->m_navigationBar = CreateNavigationBar(CAttributes()
    .Set("name", "navBar")
    .Set("title", "Template")
    .Set("rightButtonCaption", "Go to")
    .Set("x1",      0)
    .Set("y1",      0)
    .Set("width",   "100%")
    .Set("height",  "10%"));

  // Setting event handler for handling navigation bar click
  this->m_navigationBar->SetEventHandler("clicknavigationbar", this, &CTemplateView::OnClickedNavigationBar);

  // Add navigation bar in view
  this->AddChild(this->m_navigationBar);

  // Create tool bar
  //this->m_toolBar = CreateToolBar(CAttributes()
  //  .Set("name",    "toolbar")
  //  .Set("x1",      "0%")
  //  .Set("y1",      "10%")
  //  .Set("width",   "100%")
  //  .Set("height",  "10%"));

  //// Create tool bar button for New, Edit, Remove and View
  //CToolBarButtonPtr toolButton = CreateToolBarButton(CAttributes()
  //  .Set("name",    "ToolBarNewItem")
  //  .Set("caption", "New"));

  //this->m_toolBar->AddChild(toolButton);

  //toolButton = CreateToolBarButton(CAttributes()
  //  .Set("name",    "ToolBarEditItem")
  //  .Set("caption", "Edit")
  //  .Set("normal.image","button_up.png"));

  //this->m_toolBar->AddChild(toolButton);

  //toolButton = CreateToolBarButton(CAttributes()
  //  .Set("name",    "ToolBarDeleteItem")
  //  .Set("caption", "Delete"));

  //this->m_toolBar->AddChild(toolButton);

  //toolButton = CreateToolBarButton(CAttributes()
  //  .Set("name",    "ToolBarViewItem")
  //  .Set("caption", "View"));

  //this->m_toolBar->AddChild(toolButton);

  //// Setting event handler for handling toolbar click
  //this->m_toolBar->SetEventHandler("clicktoolbar", this, &CTemplateView::OnClickedToolBar);

  //// Add tool bar in view
  //this->AddChild(this->m_toolBar);

  CViewPtr viewButton = CreateView(CAttributes()
    .Set("x1", 0)
    .Set("y1", "11%")
    .Set("width", "100%")
    .Set("height", "10%")
    .Set("backgroundColour", CColour(COLOUR_PAGE)));

  this->AddChild(viewButton);

  CButtonPtr buttonItem = CreateButton(CAttributes()
    .Set("name", "buttonNew")
    .Set("x1",    "0%")
    .Set("y1",    "11%")
    .Set("width", "24%")
    .Set("caption", "New"));
  viewButton->AddChild(buttonItem);
  buttonItem->SetEventHandler("click", this, &CTemplateView::OnClickedNew);

  buttonItem = CreateButton(CAttributes()
    .Set("name", "buttonEdit")
    .Set("x1",    "25%")
    .Set("y1",    "11%")
    .Set("width", "24%")
    .Set("caption", "Edit"));

  viewButton->AddChild(buttonItem);
  buttonItem->SetEventHandler("click", this, &CTemplateView::OnClickedEdit);
  

  buttonItem = CreateButton(CAttributes()
    .Set("name", "buttonDelete")
    .Set("x1",    "50%")
    .Set("y1",    "11%")
    .Set("width", "24%")
    .Set("caption", "Delete"));

  viewButton->AddChild(buttonItem);
  buttonItem->SetEventHandler("click", this, &CTemplateView::OnClickedRemove);

  buttonItem = CreateButton(CAttributes()
    .Set("name", "buttonEdit")
    .Set("x1",    "75%")
    .Set("y1",    "11%")
    .Set("width", "24%")
    .Set("caption", "View"));

  viewButton->AddChild(buttonItem);
  buttonItem->SetEventHandler("click", this, &CTemplateView::OnClickedView);


  this->m_table = CreateTable(CAttributes()
    .Set("name",    "TablePages")
    .Set("x1",      "5%")
    .Set("y1",      "23%")
    .Set("width",   "90%")
    .Set("height",  "75%"));

  // Setting event handler for handling table selected row
  this->m_table->SetEventHandler("selectrow", this, &CTemplateView::OnSelectedTable);

  //if(this->m_templatePages)
  //for(int rowIndex = 0; rowIndex < 10; rowIndex++)
  /*{
    CTableItemPtr tableItem = CreateTableItem(CAttributes()
      .Set("width",   "100%")
      .Set("height",  "50"));

    char tmp[0xff];
    snprintf(tmp, 0xff, "LabelTable");

    CLabelPtr label = CreateLabel(CAttributes()
      .Set("name",    CString(tmp))
      .Set("caption", "template1")
      .Set("width", "100%")
      .Set("height", "100%")
      );

    tableItem->AddChild(label);

    this->m_table->AddChild(tableItem);

    this->m_tableRows.push_back(tableItem);
  }*/

  this->m_alretDlg = CreateAlertDialog(CAttributes()
    .Set("name",                    "AlertDialog")
    .Set("title",                   "Warning")
    .Set("message",                 "Would you need to delete this template?")
    .Set("positiveButtonCaption",   "Yes")
    .Set("negativeButtonCaption",   "No"));


  this->m_alretDlg->SetEventHandler("dismissalertdialog", this, &CTemplateView::OnAlertButtonClick);

  Refresh();
  

  // Add table in view
  this->AddChild(this->m_table);
}

void CTemplateView::Refresh()
{
  int iPages = this->m_templatePages->CountPages();

  int iRows = (int)this->m_tableRows.size();
  int index = 0;
  // Remove all row in table
  for(index = 0; index < iRows; index++)
    this->m_table->RemoveChild(this->m_tableRows[index]);

  // **** Find the method to delete auto pointer ****
  this->m_tableRows.clear();

  for(index = 0; index < iPages; index++)
  {
    //CTableItemPtr tableItem = CreateTableItem(CAttributes()
    //  .Set("width",   "100%")
    //  .Set("height",  "50"));

    //char tmp[0xff];
    //snprintf(tmp, 0xff, "LabelTable%d", index);

    //CLabelPtr label = CreateLabel(CAttributes()
    //  .Set("name",    CString(tmp))
    //  .Set("caption", this->m_templatePages->GetPage(index)->GetName().c_str())
    //  .Set("width", "100%")
    //  .Set("height", "100%")
    //  );

    //tableItem->AddChild(label);

		CTableItemPtr tableItem =  CreateTemplateItem(this->m_templatePages->GetPage(index));

    this->m_table->AddChild(tableItem);

    this->m_tableRows.push_back(tableItem);
  }

  //this->m_table->SetAttribute<int>("selected", 0);
}

void CTemplateView::ShowView(bool show)
{
  this->SetAttribute("visible", show);
}


bool CTemplateView::OnClickedNew(CButton* button)
{
  ControlCommand::GetInstance()->OnNew(ControlCommand::TEMPLATE_CTRL);
  this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATETEMPLATEVIEW_T);
  return true;
}


bool CTemplateView::OnClickedEdit(CButton* button)
{
  if(this->m_currentRow != -1)
  {
    ControlCommand::GetInstance()->OnEdit(ControlCommand::TEMPLATE_CTRL);
    this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATETEMPLATEVIEW_T);
  }
  return true;
}


void CTemplateView::OnClickedNext()
{

}


void CTemplateView::OnSelectedTemplate()
{

}


bool CTemplateView::OnClickedView(CButton* button)
{
  ControlCommand::GetInstance()->OnViewDetail(ControlCommand::TEMPLATE_CTRL);
  this->m_parrentWnd->OnShowView(this, IViewCtrl::PREVIEWDETIALVIEW_T);
  return true;
}


bool CTemplateView::OnClickedRemove(CButton* button)
{
  if(this->m_currentRow != -1 && this->m_templatePages->CountPages() > 0)
  {
    this->m_alretDlg->Show();
    //this->m_templatePages->RemovePage(this->m_currentRow);
    //// Save file
    //std::vector<CTableItemPtr>::iterator removeTemplate = this->m_tableRows.erase(this->m_tableRows.begin() + this->m_currentRow);
    //this->m_table->RemoveChild((*removeTemplate));
    //this->m_currentRow = -1;
    ////Refresh();
  }
  return true;
}


IPage* CTemplateView::GetCurSelected()
{
	return this->m_templatePages->GetPage(this->m_currentRow);
}

bool UI::CTemplateView::OnClickedNavigationBar( CNavigationBar *naviBar, int button )
{
  if(this->m_currentRow != -1)
  {
    ControlCommand::GetInstance()->OnNew(ControlCommand::CHECKLIST_CTRL);
    this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATECHECKLISTVIEW_T);
  }
  return true;
}

bool UI::CTemplateView::OnClickedToolBar( CToolBar* toolBar, int button )
{
  switch(button)
  {
  case 0: // New button
    {
      ControlCommand::GetInstance()->OnNew(ControlCommand::TEMPLATE_CTRL);
      this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATETEMPLATEVIEW_T);
    }
    break;
  case 1: // Edit button
    {
      if(this->m_currentRow != -1)
      {
        ControlCommand::GetInstance()->OnEdit(ControlCommand::TEMPLATE_CTRL);
        this->m_parrentWnd->OnShowView(this, IViewCtrl::CREATETEMPLATEVIEW_T);
      }
    }
    break;
  case 2: // Remove button
    {
      if(this->m_currentRow != -1 && this->m_templatePages->CountPages() > 0)
      {
        this->m_templatePages->RemovePage(this->m_currentRow);
        // Save file
        this->m_currentRow = -1;
        Refresh();
      }
    }
    break;
  case 3: // View button
    {
      ControlCommand::GetInstance()->OnViewDetail(ControlCommand::TEMPLATE_CTRL);
      this->m_parrentWnd->OnShowView(this, IViewCtrl::PREVIEWDETIALVIEW_T);
    }
    break;
  default: break;
  }
  
  
  return true;
}

bool UI::CTemplateView::OnSelectedTable( CTable* table, int tab )
{
  this->m_currentRow = tab;
  return true;
}

void UI::CTemplateView::AppendPage( IPage* newPage )
{
  this->m_templatePages->AppendPage(dynamic_cast<IPage*>(newPage->Clone()));
}

void UI::CTemplateView::EditPage( IPage* newPage )
{
  if(this->m_currentRow != -1)
  {
    IPage* currentPage = this->m_templatePages->GetPage(this->m_currentRow);
    currentPage->ClearItems();
    currentPage->CopyPage(newPage);
  }
}

bool UI::CTemplateView::OnAlertButtonClick( CAlertDialog* dialog, EAlertDialogButton button )
{
  switch(button)
  {
  case IWNUI_ALERT_DIALOG_POSITIVE:
    {
      this->m_templatePages->RemovePage(this->m_currentRow);
      // Save file
      this->m_table->RemoveChild(m_tableRows[this->m_currentRow]);
			this->m_tableRows.erase(this->m_tableRows.begin() + this->m_currentRow);
      this->m_currentRow = -1;
      //Refresh();
    }
    break;
  case IWNUI_ALERT_DIALOG_NEGATIVE:
    break;
  case IWNUI_ALERT_DIALOG_NEUTRAL:
    break;
  default: break;
  }
  return true;
}

IwNUI::CTableItemPtr UI::CTemplateView::CreateTemplateItem( IPage* page )
{
	CTableItemPtr tableItem = CreateTableItem(CAttributes()
		.Set("width",   "100%")
		.Set("height",  "60"));

	CLabelPtr label = CreateLabel(CAttributes()
		.Set("name",    "LabelTemplateName")
		.Set("caption", page->GetName().c_str())
		.Set("y1", "0%")
		.Set("width", "100%")
		.Set("height", "60%")
		.Set("fontSize", 12)
		);

	tableItem->AddChild(label);

	std::string lastBuilt = "Last built : " + page->GetTemplateTime();

	label = CreateLabel(CAttributes()
		.Set("name",    "LabelTemplateName")
		.Set("caption", lastBuilt.c_str())
		.Set("y1", "60%")
		.Set("width", "100%")
		.Set("height", "40%")
		.Set("fontSize", 6)
		);

	tableItem->AddChild(label);

	return tableItem;
}

